/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject.matchQuestion;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;

import ru.irenproject.AbstractQuestionEditor;
import ru.irenproject.Action;
import ru.irenproject.Question;
import ru.irenproject.TestInputException;
import ru.irenproject.matchQuestion.MatchQuestionProtos.MatchQuestionEditorShape;
import ru.irenproject.pad.Pad;
import ru.irenproject.pad.PadEditor;
import ru.irenproject.store.Event;
import ru.irenproject.store.Stash;
import ru.irenproject.store.Thing;

import com.google.protobuf.Message;

public final class MatchQuestionEditor extends AbstractQuestionEditor {
  public static final class DeleteRowInput {
    public int index;
  }

  public static final class MoveRowInput {
    public int index;
    public boolean forward;
  }

  public static final class SetOptionsInput {
    public Integer pairLimit;
    public Integer distractorLimit;
  }

  @Stash private MatchQuestionProtos.MatchQuestionEditor.Builder $;

  @Inject MatchQuestionEditor() {}

  @Override public MatchQuestion question() {
    return get($.getQuestion(), MatchQuestion.class);
  }

  @Override protected void setQuestion(Question question) {
    Validate.isInstanceOf(MatchQuestion.class, question);
    $.setQuestion(question.id());
    $.setFormulationEditor(PadEditor.create(question().formulation()).id());
    syncItemEditors();
    question().addListener(this);
  }

  private void syncItemEditors() {
    $.clearLeftEditor();
    for (Pad pad : question().leftPadList()) {
      $.addLeftEditor(PadEditor.open(pad).id());
    }

    $.clearRightEditor();
    for (Pad pad : question().rightPadList()) {
      $.addRightEditor(PadEditor.open(pad).id());
    }
  }

  @Override protected void handleEvent(Event e, Thing source) {
    if (e instanceof MatchQuestion.ChangeEvent) {
      syncItemEditors();
      markForRender();
    }
  }

  @Override public Message render() {
    MatchQuestion q = question();
    MatchQuestionEditorShape.Builder b = MatchQuestionEditorShape.newBuilder()
        .setFormulationEditor($.getFormulationEditor())
        .addAllLeftEditor($.getLeftEditorList())
        .addAllRightEditor($.getRightEditorList())
        .setMaxRows(MatchQuestion.MAX_ROWS)
        .setHasDefaultOptions(q.hasDefaultOptions());
    if (q.hasPairLimit()) {
      b.setPairLimit(q.pairLimit());
    }
    if (q.hasDistractorLimit()) {
      b.setDistractorLimit(q.distractorLimit());
    }
    return b.build();
  }

  @Action(badInput = TestInputException.class)
  public void doAddPair() {
    question().addPair();
  }

  @Action(badInput = TestInputException.class)
  public void doAddDistractor() {
    question().addDistractor();
  }

  @Action(badInput = TestInputException.class)
  public void doDeleteRow(DeleteRowInput in) {
    question().deleteRow(in.index);
  }

  @Action(badInput = TestInputException.class)
  public void doMoveRow(MoveRowInput in) {
    question().moveRow(in.index, in.forward);
  }

  @Action(badInput = TestInputException.class)
  public void doSetOptions(SetOptionsInput in) {
    if (in.pairLimit == null) {
      question().clearPairLimit();
    } else {
      question().setPairLimit(in.pairLimit);
    }

    if (in.distractorLimit == null) {
      question().clearDistractorLimit();
    } else {
      question().setDistractorLimit(in.distractorLimit);
    }
  }
}
